Skip to main content

PostgreSQL Setup

Installation of PostgreSQL

  • run this Command in Linux Terminal to install PostgreSQL
        sudo apt install postgresql postgresql-contrib

Start and Enable PostgreSQL

  • run following commands to start and enabale PostgreSQL
      sudo systemctl start postgresql
    sudo systemctl enable postgresql

Password Protection


  • Switch to the PostgreSQL User

      sudo -i -u postgres
  • Open PostgreSQL terminal

      psql
  • set Password

      ALTER USER postgres WITH PASSWORD 'Root@1142';
postgres User's Password

Note Down postgres Password. It will br the main Admin of PostgreSQL. So It's Crucial

  • Close the PostgreSQL terminal

        \q
  • Exit from posgres user

        exit

Allow Password Authentication

  • Edit the pg_hba.conf file:

      sudo nano /etc/postgresql/*/main/pg_hba.conf
  • Find this line:

    local   all   postgres   peer
  • change this line

    local   all   postgres   md5

Restart PostgreSQL

  sudo systemctl restart postgresql

Database Creation


  • To Login

        psql -U postgres -h localhost
  • Create Database

        CREATE DATABASE mydatabase;
  • Verify Database

        \l

Create Users


  • Create user
        CREATE USER myuser WITH PASSWORD 'mypassword';
  • list users
    \du

Give Previlidges


  • Give full access
    GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;